// pages/certification/certification.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: null, //电话号码
    code: null, //验证码
    disabled: false,
    send: '获取验证码',
    time: 60,
    status: null
  },
  onLoad: function(options) {
    // console.log(options)
    if (options.status) {
      this.setData({
        status: options.status
      })
    } else {
      return
    }
  },
  //输入手机号码
  phoneInput(e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },

  //获取验证码
  getCode() {
    let that = this;
    let phoneNum = that.data.phoneNum;
    if (!phoneNum) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      })
    } else {
      let regText = /^[1][3,4,5,7,8,9][0-9]{9}$/;
      if (!regText.test(phoneNum)) {
        wx.showToast({
          title: '请输入正确的手机号码',
          icon: 'none'
        })
      } else {
        that.setData({
          disabled: true
        })
        wx.request({
          url: 'https://api.mokyun.com/pesticide/verify',
          method: 'POST',
          header: {
            'Accept': 'application/json',
            'Authorization': 'Bearer ' + wx.getStorageSync('token'),
            'content-type': 'application/json',
            'APPID': 32
          },
          data: {
            tel: phoneNum
          },
          success: (res => {
            console.log(res)
          })
        })
        let t1 = setInterval(function() {
          let time = that.data.time;
          time--;
          that.setData({
            time: time,
            send: `已发送(${time})`,
          });
          if (that.data.time <= 0) {
            clearInterval(t1);
            that.setData({
              send: '获取验证码',
              time: 60,
              disabled: false
            })
          }
        }, 1000)
      }
    }
  },

  //输入验证码
  codeInput(e) {
    this.setData({
      code: e.detail.value
    })
  },

  //提交
  submit() {
    let that = this;
    let phoneNum = that.data.phoneNum;
    let code = that.data.code
    if (!phoneNum || !code) {
      wx.showToast({
        title: '请补全信息',
        icon: 'none'
      })
    } else {
      wx.request({
        url: 'https://api.mokyun.com/pesticide/register',
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'Authorization': 'Bearer ' + wx.getStorageSync('token'),
          'content-type': 'application/json',
          'APPID': 32
        },
        data: {
          tel: phoneNum,
          code: code
        },
        success: (res => {
          // console.log(res)
          if (res.data === 'success') {
            if (that.data.status) {
              wx.showToast({
                title: '更改成功',
                success: (res) => {
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 1
                    })
                  }, 1500)
                }
              })
            } else {
              wx.showToast({
                title: '注册成功',
                success: (res) => {
                  setTimeout(() => {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  }, 1500)
                }
              })
            }
          } else {
            wx.showToast({
              title: '验证码错误',
              icon: "none"
            })
          }
        })
      })
    }
  }
})